package com.pjhouse.core.user.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.pjhouse.core.common.domain.Result;
import com.pjhouse.core.common.util.ExceptionUtils;
import com.pjhouse.core.user.domain.UserDTO;
import com.pjhouse.core.user.persistence.UserRepository;
import com.pjhouse.core.user.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService{

	@Resource
	private UserRepository userRepository;
	@Override
	public Result<UserDTO> getById(long id) {
		try {
			UserDTO user = userRepository.queryById(id);
			if(user == null)
				return Result.ofNull();
			return Result.of(user);
		} catch (Exception e) {
			return ExceptionUtils.handleException(e);
		}
	}

	@Override
	public Result<UserDTO> getByPhone(String phone) {
		try {
			UserDTO user = userRepository.queryByPhone(phone);
			if(user == null)
				return Result.ofNull();
			return Result.of(user);
		} catch (Exception e) {
			return ExceptionUtils.handleException(e);
		}
	}

	@Override
	public Result<Long> addUser(UserDTO user) {
		try {
			return Result.of(userRepository.insert(user));
		} catch (Exception e) {
			return ExceptionUtils.handleException(e);
		}
	}

	@Override
	public Result<Void> modifyUser(UserDTO user) {
		try {
			userRepository.update(user);
			return Result.ofNull();
		} catch (Exception e) {
			return ExceptionUtils.handleException(e);
		}
	}

	@Override
	public Result<List<UserDTO>> getUserList() {
		try {
			List<UserDTO> users = userRepository.queryAll();
			return Result.of(users);
		} catch (Exception e) {
			return ExceptionUtils.handleException(e);
		}
	}

}
